﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cn.Edwon.Util {
    public class DateUtil {
        #region 获取当前日期时间在一年中的第几周
        /// <summary>
        /// 获取当前日期时间在一年中的第几周
        /// </summary>
        /// <param name="rDt"></param>
        /// <returns></returns>
        public static Int32 WeekOfYear(DateTime rDt) {
            DateTime firstDay = new DateTime(rDt.Year, 1, 1);
            int daycount = Convert.ToInt32((rDt - firstDay).TotalDays);
            int dw = Convert.ToInt32(firstDay.DayOfWeek);
            daycount += (dw == 0) ? 7 : dw;
            return Convert.ToInt32(Math.Ceiling(daycount / 7.0));
        }
        /// <summary>
        /// 获取当前日期时间在一年中的第几周
        /// </summary>
        /// <param name="rDt"></param>
        /// <returns></returns>
        public static Int32? WeekOfYear(DateTime? rDt) {
            if (rDt == null) {
                return null;
            } else {
                DateTime dt = Convert.ToDateTime(rDt);
                return WeekOfYear(dt);
            }
        }

        public static String WeekRangeText(Int32 rYear,Int32 rWeek) {
            DateTime firstDay = new DateTime(rYear, 1, 1);
            int dw = Convert.ToInt32(firstDay.DayOfWeek);
            if (dw == 0) dw = 7;
            DateTime dt1 = firstDay.AddDays((rWeek-1)*7 - dw+1);
            DateTime dt2 = firstDay.AddDays((rWeek)*7 - dw);
            return dt1.ToString("MM月dd") + "-" + dt2.ToString("MM月dd");
        }
 
        #endregion

        public static Int32 TotalWeeks(DateTime rDt) {
            DateTime firstDay = new DateTime(rDt.Year, 1, 1);
            int dw = Convert.ToInt32(firstDay.DayOfWeek);
            Int32 days = 0;
            for (int i = 1; i <= 12; i++) {
                days += DateTime.DaysInMonth(rDt.Year, i);
            }
            days += (dw == 0) ? 7 : dw;
            return Convert.ToInt32(Math.Ceiling(days / 7.0));
        }

        #region 农历
        #endregion
    }
}
